package com.iss.card.view.account;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iss.card.R;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.FunctionUtil;
import com.iss.card.common.IBackKeyListener;
import com.iss.card.common.Tools;
import com.iss.card.view.model.ResponseBean;
import com.iss.card.webservice.BaseModel;
import com.iss.card.webservice.WebServiceAsyncService;
import com.iss.card.webservice.WebServiceAsyncService.Operation;
import com.iss.card.webservice.WebServiceAsyncService.PostExecute;
import com.iss.card.webservice.WebServiceAsyncServiceHelper;
import com.iss.card.webservice.WebServiceManager.WebServiceRegistrationParameter;
import com.iss.card.webservice.WebServiceManager.WebServiceUrlPortManager;
import com.iss.card.webservice.WebServiceUtils;

public class AccountFeedBackFragment extends Fragment implements
		OnClickListener, IBackKeyListener {
	private final String TAG = AccountFeedBackFragment.class.getSimpleName();

	private Activity mContext;

	private EditText mEditFeedBack;

	private Button mReturnBut;
	private ImageButton mJobBut;
	private TextView mJobName;

	/** it is used to show software keyboard automatically. */
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			mEditFeedBack.requestFocus();
			InputMethodManager inputManager = (InputMethodManager) mEditFeedBack
					.getContext()
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputManager.showSoftInput(mEditFeedBack, 0);
		}
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		this.mContext = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.feed_back_layout, null);
		initFeedBack(layout);
		mHandler.sendEmptyMessageDelayed(0, 50);
		return layout;
	}

	private void initFeedBack(LinearLayout layout) {
		mEditFeedBack = (EditText) layout.findViewById(R.id.edit_feed_back);
		mJobName = (TextView) layout.findViewById(R.id.job_name_val);
		mReturnBut = (Button) layout.findViewById(R.id.return_button);
		mJobBut = (ImageButton) layout.findViewById(R.id.job_button);
		mReturnBut.setVisibility(View.VISIBLE);
		mJobBut.setVisibility(View.VISIBLE);
		mReturnBut.setBackgroundResource(R.drawable.return_selector);
		mJobBut.setBackgroundResource(R.drawable.feedback_send_selector);
		mReturnBut.setOnClickListener(this);
		mJobBut.setOnClickListener(this);

		mJobName.setText(R.string.more_feedback);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.return_button:

			mContext.finish();
			break;
		case R.id.job_button:

			submitFeedBack();
			break;

		default:
			break;
		}
	}

	private void submitFeedBack() {
		mEditFeedBack.setEnabled(false);
		String feedBackVal = mEditFeedBack.getText().toString();
		if (TextUtils.isEmpty(feedBackVal)) {
			Tools.showToast(mContext, R.string.toast_no_feed_back);
			return;
		}
		String[] parameterValues = { FunctionUtil.getCurrentLoginUserMobile(),
				feedBackVal };

		WebServiceAsyncService service = new WebServiceAsyncService();
		service.setParameterToAsyncService(mContext, service.getNextToken(),
				Operation.OP_SERVICE_ARG_FEED_BACK,
				WebServiceRegistrationParameter.PRODUCT_FEED_BACK,
				parameterValues,
				WebServiceUrlPortManager.ACTION_URL_PORT_FEED_BACK, this,
				WebServiceAsyncServiceHelper.TYPE_HTTP_GET_INSTREAM);
		service.setPostExecute(new PostExecute() {
			@Override
			public void onAsyncServiceComplete(int token, Object envelope,
					int index) {

				ResponseBean response = WebServiceUtils
						.getObjectResultToBaseInfo(envelope, BaseModel.class);
				if (response == null) {
					Log.e(TAG, "response is null");
					return;
				}

				if (response.getStatus().equals(
						ApplicationManager.RESPONSE_OK)) {
					Tools.showToast(mContext, R.string.toast_feed_back);
					
					new Handler().postDelayed(new Runnable() {
						public void run() {
							mContext.finish();
						}
					}, 2000);
				} else {
					mEditFeedBack.setEnabled(true);
					Tools.showToast(mContext, response.getMessage());
					return;
				}
			}
		});

	}

	@Override
	public void cancelUpdate(Object... params) {
		// TODO Auto-generated method stub

	}
}
